﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;

namespace AIC_CS
{
    public partial class BaoCaoKetQuaCongTacChinhSachTrongTamCongTacChinhSach_Edit : MyFromBase
    {
        private static string SelectedID = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindDataToDropDownList(ddlKyBaoCao, "KyBaoCao");
                ddlKyBaoCao.Items.Insert(0, new ListItem("Chọn kỳ báo cáo", "-1"));
                lblDonVi.Text = MyCommon.GetTenDonVi(Session["userid"].ToString());
                BindDataToGrid();
            }

        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {

        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            BindDataToGrid();

        }

        protected void btnThem_Click(object sender, EventArgs e)
        {

        }

        protected void grvData_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void grvData_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvData.PageIndex = e.NewPageIndex;
            
        }
        protected void grvData_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            SelectedID = e.CommandArgument.ToString().Trim();
            string sCommandName = e.CommandName;
            if ((sCommandName == "Detail"))
            {
                string sql = "Select KyBaoCaoID_DM,BaoCaoSo From tblBaoCaoKetQuaCongTacChinhSachTrongTamCongTacChinhSach Where id='" + SelectedID + "' ";
                SqlDataReader myReader = SqlHelper.ExecuteReader(CommandType.Text, sql);
                myReader.Read();
                MyReportNumber = myReader["BaoCaoSo"].ToString();
                MyReportPeriod = myReader["KyBaoCaoID_DM"].ToString();
                MySwitchCase = e.CommandName;
                MyReportID = SelectedID;
                Response.Redirect("PhuLuc_Edit.aspx");
            }
            if ((sCommandName == "Sua"))
            {
                string sql = "Select KyBaoCaoID_DM,BaoCaoSo From tblBaoCaoKetQuaCongTacChinhSachTrongTamCongTacChinhSach Where id='" + SelectedID + "' ";
                SqlDataReader myReader = SqlHelper.ExecuteReader(CommandType.Text, sql);
                myReader.Read();
                MyReportNumber = myReader["BaoCaoSo"].ToString();
                MyReportPeriod = myReader["KyBaoCaoID_DM"].ToString();
                MySwitchCase = e.CommandName;
                MyReportID = SelectedID;
                Response.Redirect("PhuLuc_Edit.aspx");
            }
            
        }
        protected void grvData_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            RowDataBound(sender, e);
        }
        protected void ddlKyBaoCao_SelectedIndexChanged(object sender, EventArgs e)
        { 
        }
        protected void BindDataToDropDownList(DropDownList BindDropDown, string MaDM)
        {
            List<SqlParameter> arrParams = new List<SqlParameter>();
            arrParams.Add(SqlHelper.CreateParameterObject("@MaDM", SqlDbType.NVarChar, ParameterDirection.Input, MaDM));
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, "SELECT * FROM dbo.tblDanhMuc WHERE MaDM=@MaDM", arrParams);
            BindDropDown.DataSource = dt;
            BindDropDown.DataTextField = "TenDM";
            BindDropDown.DataValueField = "ID";
            BindDropDown.DataBind();
        }
        private void BindDataToGrid()
        {
            bool IsEmpty = false;
            List<SqlParameter> arrParams = new List<SqlParameter>();
            if (ddlKyBaoCao.SelectedItem.Text.Trim()=="Chọn kỳ báo cáo")
            arrParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, "%" + ""+ "%"));
            else
                arrParams.Add(SqlHelper.CreateParameterObject("@KyBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, "%" + ddlKyBaoCao.SelectedValue.ToString().Trim()+ "%"));
            arrParams.Add(SqlHelper.CreateParameterObject("@SoBaoCao", SqlDbType.NVarChar, ParameterDirection.Input, "%" + txtReportNumber.Text.Trim() + "%"));




            string sql = "select a.id,ROW_NUMBER() over (order by (select 1)) as SoThuTu,a.TenBaoCao as TenBaoCao, a.BaoCaoSo as BaoCaoSo, b.TenDM as KyBaoCao,  CONVERT(VARCHAR(10),a.createdate,103) as Created, CONVERT(VARCHAR(10),a.updatedate,103) as Updated from tblBaoCaoKetQuaCongTacChinhSachTrongTamCongTacChinhSach a, tblDanhMuc b" +
            " where a.KyBaoCaoID_DM=b.id And KyBaoCaoID_DM Like @KyBaoCao And BaoCaoSo Like @SoBaoCao ";

            
            DataTable dt = SqlHelper.ExecuteDataTable(CommandType.Text, sql, arrParams);
            if (dt.Rows.Count == 0)
            {
                IsEmpty = true;

                DataRow dr = dt.NewRow();
                dr[0] = 0;
                dt.Rows.InsertAt(dr, 0);

            }

            grvData.DataSource = dt;
            grvData.DataBind();
            if (IsEmpty)
            {
                grvData.Rows[0].Visible = false;
            }
        }
    }
}
